package com.xiaoshi.system.service.impl;

import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xiaoshi.system.entity.SysMenu;
import com.xiaoshi.system.entity.SysUser;
import com.xiaoshi.exception.PlatformException;
import com.xiaoshi.system.mapper.SysUserMapper;
import com.xiaoshi.system.service.ISysMenuService;
import com.xiaoshi.system.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 用户表 服务实现类
 * </p>
 *
 * @author sen
 * @since 2021-06-28
 */
@Service
public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUser> implements ISysUserService {



    @Autowired
    private ISysMenuService menuService;


    @Override
    public SysUser findByName(String username) {
        if(StringUtils.isEmpty(username))   throw new PlatformException("获取用户信息 username为空");
        return this.baseMapper.findByName(username);
    }

    /**
     * 通过手机号 查找会员
     * @param phone
     * @return
     */
    @Override
    public SysUser findUserByPhone(String phone) {
        return this.baseMapper.findUserByPhone(phone);
    }

    /**
     * 通过 id 找到公司
     * @param fid
     * @return
     */
//    @Override
//    public SysFirmVo findFirmById(Integer fid) {
//        if(fid  == null ) throw new PlatformException("公司 fid 为空");
//        return this.baseMapper.findFirmById(fid);
//    }


    /**
     * 获取权限
     * @param username
     * @return
     */
    @Override
    public String findUserPermissions(String username) {
        List<SysMenu> userPermissions = menuService.findUserPermissions(username);

        return userPermissions.stream().map(SysMenu::getPerms).collect(Collectors.joining(","));

    }

}
